//
//  DDGoodsManager.m
//  Dingdong
//
//  Created by 程强 on 16/11/25.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDGoodsManager.h"
#import "DDGoodsObj.h"
#import "DDGoodsDetailsObj.h"
#import "DDCommentSet.h"

@implementation DDGoodsManager
- (DSASubject *)getGoodsListByCategory:(NSNumber *)category_id pageNumber:(NSInteger)pageNo pageSize:(NSInteger)size
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/goods/getPage.do", ServerUrl];
    
    NSDictionary *body = @{@"id":category_id,@"pageNo":@(pageNo),@"pageSize":@(size),@"sort":@""};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDGoodsSet *set = [DDGoodsSet modelWithDictionary:rdic[@"data"]];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)goodsSort:(NSNumber *)category_id byKeywords:(NSString *)word pageNumber:(NSInteger)pageNo pageSize:(NSInteger)size
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/goods/getPage.do", ServerUrl];
    
    NSDictionary *body = @{@"id":category_id,@"pageNo":@(pageNo),@"pageSize":@(size),@"sort":word};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDGoodsSet *set = [DDGoodsSet modelWithDictionary:rdic[@"data"]];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)goodsSearchBy:(NSString *)key sort:(NSString *)sort pageNo:(NSInteger)page size:(NSInteger)size
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/goods/getPage.do", ServerUrl];
    
    NSDictionary *body = @{@"keyword":key,@"pageNo":@(page),@"pageSize":@(size),@"sort":sort};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDGoodsSet *set = [DDGoodsSet modelWithDictionary:rdic[@"data"]];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)goodsDetails:(NSNumber *)goods_id
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/goods/get.do", ServerUrl];
    
    NSDictionary *body = @{@"id":goods_id};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDGoodsDetailsObj *detailsObj = [DDGoodsDetailsObj modelWithDictionary:rdic[@"data"]];
        [subject sendNext:detailsObj];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)getCommentList:(NSNumber *)goods_id grade:(NSInteger)grade pageNo:(NSInteger)pageNo pageSize:(NSInteger)size
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/comment/getPage.do", ServerUrl];
    
    NSDictionary *body = @{@"goodsId":goods_id,@"grade":@(grade),@"type":@1,@"pageNo":@(pageNo),@"pageSize":@(size)};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDCommentSet *commentSet = [DDCommentSet modelWithDictionary:rdic[@"data"]];
        [subject sendNext:commentSet];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)checkGoodsIsCollection:(NSNumber *)goods_id
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/favorite/isfavorite.do", ServerUrl];
    
    NSDictionary *body = @{@"id":goods_id};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        NSNumber *isCollection = rdic[@"data"];

        [subject sendNext:isCollection];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
@end
